#coding=utf8
import itchat
import requests

# 8edce3ce905a4c1dbb965e6b35c3834d
# eb720a8970964f3f855d863d24406576
# 1107d5601866433dba9599fac1bc0083
# 71f28bf79c820df10d39b4074345ef8c
KEY = '71f28bf79c820df10d39b4074345ef8c'

def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key'    : KEY,
        'info'   : msg,
        'userid' : 'wechat-robot',
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return


@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
    toUserName=msg['User']['UserName']
    if msg['User']['NickName'] == '张磊':
        reply = get_response(msg['Text']) or '你是大逗逼吗？'
        itchat.send(reply,toUserName)

# 为了让实验过程更加方便（修改程序不用多次扫码），我们使用热启动
itchat.auto_login(hotReload=True)
itchat.run()